from django.shortcuts import render
from rest_framework.generics import ListAPIView
from hospitals.models import DepartmentModel, HospitalModel
from hospitals.serializers import DepartmentModelSer, HospitalModelSer
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from doctors.models import DoctorModel, EvaluateModel, AnswerModel
from doctors.serializers import DoctorModelSerializer
from rest_framework.response import Response
from rest_framework import status


# 首页科室展示列表
class DepartmentListAPIView(ListAPIView):
    queryset = DepartmentModel.objects.filter(is_show=True, is_deleted=False, direction=0).order_by('id')
    serializer_class = DepartmentModelSer


class DepartmentListAllAPIView(ListAPIView):
    queryset = DepartmentModel.objects.filter(is_show=True, is_deleted=False).order_by('id')
    serializer_class = DepartmentModelSer


class Page(PageNumberPagination):
    page_size = 3
    page_query_param = 'page'


class HospitalListAPIView(ListAPIView):
    serializer_class = HospitalModelSer
    pagination_class = Page

    def get_queryset(self):
        department_id = int(self.kwargs.get('departmentID', 0))
        query_set = HospitalModel.objects.all().order_by('id')
        if department_id > 0:
            query_set = query_set.filter(department=department_id)
        return query_set


class HospitalDetailAPIView(APIView):
    def get(self, request, id):
        hospital = HospitalModel.objects.filter(id=id).first()
        if hospital:
            doctor_list = DoctorModel.objects.filter(hospital=hospital)
            doctor_id_list = [item.id for item in doctor_list]
            evaluate_list = EvaluateModel.objects.filter(doctor_id__in=doctor_id_list).order_by('-star')
            evaluate_doctor_id_list = [item.doctor_id for item in evaluate_list]
            data = []
            sort = 1
            for i in evaluate_doctor_id_list:
                doctor_info = DoctorModel.objects.filter(id=i).first()
                evaluate_star_list = EvaluateModel.objects.filter(doctor_id=i)
                star_all = [float(item.star) for item in evaluate_star_list]
                # 查询医生每月的回答量  待开发
                answer_list = AnswerModel.objects.filter(doctor_id=i)
                answer = 0
                if len(answer_list) > 0:
                    answer = len(answer_list)
                else:
                    answer = 0
                star = sum(star_all) / len(star_all)
                doctor_info_dict = {
                    'id': doctor_info.id,
                    'sort': sort,
                    'avatar': "http://127.0.0.1:8000/uploads/" + str(doctor_info.avatar),
                    'name': str(doctor_info.name),
                    'title': str(doctor_info.get_title_display()),
                    'hospital': str(doctor_info.hospital),
                    'department': str(doctor_info.department),
                    'star': f'{star:.2f}',
                    'answer': answer,
                    'profile': str(doctor_info.profile),
                    'price': str(doctor_info.price),
                }
                sort += 1
                data.append(doctor_info_dict)
            hospital_info = HospitalModelSer(hospital)
            return Response({'hospital': hospital_info.data, 'doctor': data}, status=status.HTTP_200_OK)
        return Response({'error': '未找到医院'}, status=status.HTTP_404_NOT_FOUND)
